package startup;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import controller.ShortcutController;
import lombok.extern.slf4j.Slf4j;
import view.HomePage;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;

/**
 * @author zhangpeiwen
 * @since 2020/10/1
 */
@Slf4j
public class Main {

    private HomePage homePage = Singleton.get(HomePage.class);
    private static ShortcutController shortcutController = Singleton.get(ShortcutController.class);

    public static void main(String[] args) {
        shortcutController.bindGlobalWakeOrHideShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

        ThreadUtil.execute(()->{
            String cp = "";
            while (true){
                if(!cp.equals(getClipboardString()) && ObjectUtil.isNotNull(getClipboardString())){
                    cp = getClipboardString();
                    JLabel label = new JLabel(StrUtil.sub(cp, 0, 50));
                    Singleton.get(HomePage.class).add(label);
                    log.info("new copy is " + cp);
                }
            }
        });

    }

    public static String getClipboardString() {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 获取剪贴板中的内容
        Transferable trans = clipboard.getContents(null);

        if (trans != null) {
            // 判断剪贴板中的内容是否支持文本
            if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    // 获取剪贴板中的文本内容
                    String text = (String) trans.getTransferData(DataFlavor.stringFlavor);
                    return text;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
